//
//  TFEmotionView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
typealias TFExpressionBlock = (_ expressionName: String ) -> Void
class TFEmotionView: UIView,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collection.dequeueReusableCell(withReuseIdentifier: "ExpressionCell", for: indexPath) as! TFExpressionCell
        let imageName = self.dataSource[indexPath.row]
        cell.expressionImageView.image = UIImage.init(named: imageName)
     
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.expressionBlock != nil {
            let str = self.dataSource[indexPath.row]
            self.expressionBlock!(str)
        }
    }
    
    @objc public var collection:UICollectionView!
    @objc public var deleteBtn:TF_ExpressionCloseButton!
    @objc public var expressionBlock:TFExpressionBlock?
    @objc public var sendBtn:UIButton!
    var dataSource:[String] = ["slime","laugh","love","tongue","daze","handclap","titter","handssors","six","heart","melon","silly","like","dog","expect","flower"];

    override init(frame: CGRect) {
        super.init(frame: frame)

        let layout = UICollectionViewFlowLayout.init()
            layout.itemSize = CGSize(width: 50, height: 50)
             layout.scrollDirection = .vertical
          self.collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        
            self.collection.delegate = self;
            self.collection.dataSource = self;
            self.collection.backgroundColor = UIColor.white
            addSubview(self.collection)
          
           self.collection.register(TFExpressionCell.self, forCellWithReuseIdentifier: "ExpressionCell")
        
            if UIDevice.current.model.hasPrefix("iPad") {
                layout.minimumInteritemSpacing = 15
                layout.sectionInset = UIEdgeInsets(top: 10, left: 34, bottom: 10, right: 34)
            } else {
                layout.minimumInteritemSpacing = 15
                layout.sectionInset = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
            }
            self.collection.backgroundColor = UIColor.clear
            self.deleteBtn = TF_ExpressionCloseButton.init()
            self.deleteBtn.setImage(UIImage.init(named: "HTLive.bundle/largeLive_del"), for: .normal)
      
            self.deleteBtn.addTarget(self, action: #selector(deleteBtnTouchDown(btn:)), for: .touchUpInside)
 
            self.deleteBtn.layer.cornerRadius = 6;
            self.deleteBtn.layer.masksToBounds = true
            
           self.deleteBtn.backgroundColor = UIColor(red: 241/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1.0)
      
           addSubview(self.deleteBtn)

            
        self.sendBtn = UIButton.init()
        self.sendBtn.setTitle("发送", for: .normal)
        self.sendBtn.setTitleColor(UIColor.white, for: .normal)
        self.sendBtn.backgroundColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        addSubview(self.sendBtn)

        self.sendBtn.layer.cornerRadius = 6;
        self.sendBtn.layer.masksToBounds = true;
        self.sendBtn.addTarget(self, action: #selector(sendBtnClick), for: .touchUpInside)
      
    }
    
  
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.collection.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        self.deleteBtn.frame = CGRect(x: self.collection.frame.maxX-130, y: self.frame.size.height-60, width: 56, height: 44)
        self.sendBtn.frame = CGRect(x: self.deleteBtn.frame.maxX+10, y: self.frame.size.height-60, width: 56, height: 44)

    }
    
   @objc func deleteBtnTouchDown(btn:TF_ExpressionCloseButton)  {
       if (self.expressionBlock != nil) {
           self.expressionBlock!("delete");
       }
    }
    
    @objc func sendBtnClick()  {
        if (self.expressionBlock != nil) {
            self.expressionBlock!("send");
        }
    }
   
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
